from sqlalchemy.orm import Session
from typing import Optional, List
from app.models.iaro_career.ite_detail import ITEDetail
from app.schema.ite_detail_schema import ITEDetailCreate, ITEDetailUpdate

def get_ite_by_id(db: Session, ite_id: int) -> Optional[ITEDetail]:
    return db.query(ITEDetail).filter(ITEDetail.ite_id == ite_id).first()

def get_ites(db: Session, skip: int = 0, limit: int = 100) -> List[ITEDetail]:
    return db.query(ITEDetail).offset(skip).limit(limit).all()

def create_or_update_ite(db: Session, data: ITEDetailCreate) -> ITEDetail:
    ite = get_ite_by_id(db, data.ite_id)
    if ite:
        for key, value in data.dict(exclude_unset=True).items():
            setattr(ite, key, value)
    else:
        ite = ITEDetail(**data.dict())
        db.add(ite)
    db.commit()
    db.refresh(ite)
    return ite